﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AccountSummaryViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the AccountViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.ViewModel
{
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using Business.Interfaces;
    using Hisaab.Common;
    using Hisaab.Windows.Common;

    using Microsoft.Practices.ServiceLocation;

    using Model;

    /// <summary>
    /// Defines the <seealso cref="AccountSummaryViewModel"/> type.
    /// </summary>
    public class AccountSummaryViewModel : ViewModelBase
    {
        /// <summary>
        /// Account object
        /// </summary>
        private readonly IAccountManager _accountManager;

        /// <summary>
        /// Session manager
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// Selected account summary
        /// </summary>
        private AccountSummary _summary;

        /// <summary>
        /// Backing fiel
        /// </summary>
        private ObservableCollection<AccountMonthlyTotal> _monthlyTotals;

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountSummaryViewModel"/> class.
        /// </summary>
        /// <param name="account">The account.</param>
        public AccountSummaryViewModel(Account account) :
            this(
            account,
            ServiceLocator.Current.GetInstance<IAccountManager>(),
            ServiceLocator.Current.GetInstance<ISession>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountSummaryViewModel"/> class.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <param name="accountManager">The account manager.</param>
        /// <param name="session">The session</param>
        public AccountSummaryViewModel(Account account, IAccountManager accountManager, ISession session)
        {
            Contract.Requires<ArgumentNullException>(accountManager != null);
            Contract.Requires<ArgumentNullException>(session != null);

            _accountManager = accountManager;
            SelectedAccount = account;
            _session = session;

            if (account != null)
            {
                Summary = _accountManager.GetAccountSummary(account.AccountId, _session.LoggedUser).Map<Business.Entities.AccountSummary, AccountSummary>();
                var balanceSummary = _accountManager.GetMonthlyBalanceSummary(SelectedAccount.AccountId, _session.LoggedUser)
                    .Select(x => x.Map<Business.Entities.AccountMonthlyTotal, AccountMonthlyTotal>());
                MonthlyTotals = new ObservableCollection<AccountMonthlyTotal>(balanceSummary);
            }
        }

        /// <summary>
        /// Gets or sets the selected account.
        /// </summary>
        /// <value>
        /// The selected account.
        /// </value>
        public Account SelectedAccount { get; set; }

        /// <summary>
        /// Gets or sets Summary.
        /// </summary>
        public AccountSummary Summary
        {
            get
            {
                return _summary;
            }

            set
            {
                if (_summary != value)
                {
                    _summary = value;
                    OnPropertyChanged("Summary");
                }
            }
        }

        /// <summary>
        /// Gets or sets the monthly totals.
        /// </summary>
        /// <value>
        /// The monthly totals.
        /// </value>
        public ObservableCollection<AccountMonthlyTotal> MonthlyTotals
        {
            get
            {
                return _monthlyTotals;
            }

            set
            {
                _monthlyTotals = value;
                OnPropertyChanged("MonthlyTotals");
            }
        }
    }
}